﻿using System.Data;
using System.Data.Common;
using System.IO;
using System.Runtime.Serialization;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using PublicDomain.Data.Csv;
using PublicDomain.Data.Csv.Metadata;

namespace PublicDomain.UnitTest.Data.Csv
{
    [TestClass]
    public class DataRowToDataRecordAdapterTest
    {
        public TestContext TestContext { get; set; }

        public void TestInitialize()
        {
        }

        [TestMethod]
        [Description("Tests all get field values methods.")]
        [DeploymentItem("DataRowToDataRecordAdapterTestSchemata.xml")]
        public void GetFieldValueTest()
        {
            CsvDataFormat csvDataFormat;
            var dataContractSerializer = new DataContractSerializer(typeof(CsvSchemata));
            using (var fileStream = new FileStream("DataRowToDataRecordAdapterTestSchemata.xml", FileMode.Open, FileAccess.Read))
            {
                var csvSchemata = (CsvSchemata)dataContractSerializer.ReadObject(fileStream);

                csvDataFormat = csvSchemata.GetCsvDataFormat("GetFieldValueTest");
            }

            DataTableMapping defaultDataTableMapping;
            var dataTable = CsvDataReaderAdapter.CreateDataTable("GetFieldValueTestTable", csvDataFormat.CsvColumnsDefinitions, out defaultDataTableMapping);

            var dataRow = dataTable.NewRow();

            dataRow.SetField<string>(dataRow.Table.Columns[0], "ABCxyz");
            dataRow.SetField<double>(dataRow.Table.Columns[1], 123.567D);

            var dataRowToDataRecordAdapter = new DataRowToDataRecordAdapter(dataRow);

            Assert.AreEqual("ABCxyz", dataRowToDataRecordAdapter.GetString(0));
            Assert.AreEqual(123.567D, dataRowToDataRecordAdapter.GetDouble(1));
        }
    }
}
